package com.ssm;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class IWidget extends AppWidgetProvider {
	private static AppWidgetManager appWidgetManager;
	Intent intent;
	PendingIntent pendingIntent;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onUpdate(context, appWidgetManager, appWidgetIds);

		if (appWidgetIds == null) {
			appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(
					context, IWidget.class));
		}
		
		this.appWidgetManager = appWidgetManager;
		UpdateService.regAppWidgetIds(context, appWidgetIds);
		
		StartUpdate(context, appWidgetIds);
		Viewing(context);
		
	}
	
	@Override
	public void onEnabled(Context context) {
		// TODO Auto-generated method stub
		super.onEnabled(context);
	}
	
	@Override
	public void onDisabled(Context context) {
		// TODO Auto-generated method stub
		super.onDisabled(context);
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);
		String action = intent.getAction();
		if (action.equals("CLICK_RELOAD")) {
			Log.d("Androes", "ACTION_CLICK_RELOAD");
			Toast.makeText(context, "Data Collecting", Toast.LENGTH_SHORT)
					.show();
			Viewing(context);
			
		} else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
			Log.d("Androes", "ACTION_APPWIDGET_UPDATE");
			Bundle extras = intent.getExtras();
			if (extras != null) {
				int[] appWidgetIds = extras
						.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
				if (appWidgetIds != null && appWidgetIds.length > 0) {
					this.onUpdate(context, AppWidgetManager
							.getInstance(context), appWidgetIds);
				}
			}
		} else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
			Log.d("Androes", "ACTION_APPWIDGET_DELETED");

			final int appWidgetId = intent.getExtras().getInt(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
				this.onDeleted(context, new int[] { appWidgetId });
			}
			/*
			 * Bundle extras = intent.getExtras(); if (extras != null &&
			 * extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)) { final
			 * int appWidgetId = extras
			 * .getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
			 * this.onDeleted(context, new int[] { appWidgetId }); }
			 */
		} else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
			Log.d("TAG", "ACTION_APPWIDGET_ENABLED");

			this.onEnabled(context);
		} else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) {
			Log.d("TAG", "ACTION_APPWIDGET_DISABLED");

			this.onDisabled(context);
		} else {
			super.onReceive(context, intent);
		}

	}

	private void StartUpdate(Context context, int[] appWidgetIds) {
		Intent updateIntent = new Intent(context, UpdateService.class);

		PendingIntent pendingIntent = PendingIntent.getService(context, 0,
				updateIntent, 0);
		if (pendingIntent == null)
			Log.e("su", "pendingIntent is null");
		else
			Log.e("su2", "pendingIntent is not null");

		Calendar cal = Calendar.getInstance();
		// Schedule alarm, and force the device awake for this update
		AlarmManager alarmManager = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);

		int delay = 2000; // 10��: 10000 / 30��: 1800000
		alarmManager.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(),
				delay, pendingIntent);
		
	}

	public static void Viewing(Context context) {
		if (context == null)
			return;

		String en="d";
		String en1="d3";
		String en2="dd";	

		ComponentName watchWidget;			
		Intent intent = new Intent(context, IdeaBoxesActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
				intent, 0);
		RemoteViews remoteView = new RemoteViews(context.getPackageName(),
				R.layout.widget_layout);
		remoteView.setOnClickPendingIntent(R.id.btn, pendingIntent);
		remoteView.setTextViewText(R.id.envi, en);
		remoteView.setTextViewText(R.id.value, en1);
		remoteView.setTextViewText(R.id.comp, en2);
		watchWidget = new ComponentName(context, IWidget.class);
		appWidgetManager.updateAppWidget(watchWidget, remoteView);
	}
}
